package com.financing.wap.web.controller.api;

import javax.servlet.http.HttpServletRequest;

import com.financing.wap.service.*;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.dto.DstArticleDTO;
import com.financing.wap.dto.DstArticleShareLogDTO;
import com.financing.wap.dto.DstUserArticleDTO;
import com.financing.wap.dto.DstUserInfoDTO;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("dstArticleShareLogController")
@RequestMapping("/api/dstArticleShareLog")
public class DstArticleShareLogController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstArticleShareLogService dstArticleShareLogService;
	
	
	@Autowired
	private IDstArticleService dstArticleService;	
	
	@Autowired
	private IDstUserArticleService dstUserArticleService;

	@Autowired
	private IDstUserInfoService dstUserInfoService;
	
	@Autowired
	private ICommonService commonService;

	@Autowired
	private IDstIntegralRuleService dstIntegralRuleService;
	
	/***
	 * 新增文章分享
	 * @param request
	 * @return
	 */
	@RequestMapping("/addDstArticleShareLog")
	@ResponseBody
	public ResultMap add(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add dstArticleShareLog  begin .......");
			//当前登录用户的openid
			String shareUserOpenid=request.getParameter("openid");
			String shareArticleCode=request.getParameter("shareArticleCode");
			String articleUseropenid=request.getParameter("articleUseropenid");
			//4q-新增分享code,分享type,拜访id
			String shareCode = request.getParameter("shareCode");
			String shareType=request.getParameter("shareType");
			String visitId=request.getParameter("visitId");
			
			if(StringUtil.isNullOrEmptyStr(shareUserOpenid,shareArticleCode,articleUseropenid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			logger.info(" shareUserOpenid:"+shareUserOpenid+",shareArticleCode:"+shareArticleCode+",articleUseropenid:"+articleUseropenid);
			
			//分享查询文章是否存在   
			DstArticleDTO art=new DstArticleDTO();
			art.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			art.setDstArticleCode(shareArticleCode);
			DstArticleDTO article = dstArticleService.queryListGetFirst(art);
			if(article.getId()==null){
				result.setStatusCode(ResultMap.STATUS_CODE_ARTICLE_USERINFO_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			//分享生成我的文章
			DstUserArticleDTO dstUserArticle = new DstUserArticleDTO();
			if(!StringUtil.isNullOrEmptyStr(articleUseropenid)){
				dstUserArticle.setUserOpenid(articleUseropenid);
			}else{
				dstUserArticle.setUserOpenid(shareUserOpenid);
			}
			dstUserArticle.setDstArticleCode(shareArticleCode);
			DstUserArticleDTO userArticle = dstUserArticleService.queryListGetFirst(dstUserArticle);
			if(userArticle.getId()==null){
				try {
					dstUserArticle.setRelArticleId(article.getId());
					dstUserArticle.setDescription(article.getDescription());
					dstUserArticle.setTitle(article.getTitle());
					dstUserArticle.setThumbnail(article.getThumbnail());
					dstUserArticle.setDstArticleContent(article.getContent());
					DTOdateSetUtil.setCreateInfo(dstUserArticle);
					DTOdateSetUtil.setUpdateInfo(dstUserArticle);
					dstUserArticle.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
					dstUserArticleService.insert(dstUserArticle);
				} catch (Exception e) {
					log.error("addDstArticleShareLog dstUserArticle error!",e);
				}
			}
			
			//如果用户不存在  直接返回
			DstUserInfoDTO dstUserInfo = new DstUserInfoDTO();
			dstUserInfo.setUserWechatOpenid(shareUserOpenid);
			dstUserInfo.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			DstUserInfoDTO userinfo = dstUserInfoService.queryListGetFirst(dstUserInfo);
			if(userinfo.getId()==null){
				logger.error("shareUserOpenid is not existent :"+shareUserOpenid);
				result.setStatusCode(ResultMap.STATUS_CODE_OPERATE_ERROE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			// 增加文章分享的记录 
			DstArticleDTO d=new DstArticleDTO();
			d.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			d.setDstArticleCode(shareArticleCode);
			d.setShareCount(1);
			DTOdateSetUtil.setUpdateInfo(d);
			commonService.addVisitArticleLog(d);
			
		    DstArticleShareLogDTO dstArticleShareLog=new DstArticleShareLogDTO();
		    dstArticleShareLog.setArticleUseropenid(articleUseropenid);
		    dstArticleShareLog.setShareArticleCode(shareArticleCode);
		    dstArticleShareLog.setShareUserOpenid(shareUserOpenid);
		    //1本人分享  2转发分享
		    if(articleUseropenid.equals(shareUserOpenid)){
		    	dstArticleShareLog.setShareWho("1");
		    }else{
		    	dstArticleShareLog.setShareWho("2");
		    }

			dstArticleShareLog.setShareCode(shareCode);
			dstArticleShareLog.setShareType(shareType);
			dstArticleShareLog.setVisitId(StringUtils.isBlank(visitId)?null:Integer.valueOf(visitId));

		    dstArticleShareLog.setArticleShareDate(DateUtil.getDate("yyyy-MM-dd"));
			int id = dstArticleShareLogService.insert(dstArticleShareLog);
			result.setAttribute("id", id);

			//积分规则  1本人分享
			if(articleUseropenid.equals(shareUserOpenid)){
				dstIntegralRuleService.execute(shareUserOpenid,"shareArticle");
			}

			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add dstArticleShareLog  end .......");
		}catch(Exception e){
			log.error("add dstArticleShareLog error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
}
